﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using OAWeb.Models;

namespace OAWeb.Controllers
{
    public class ExamsController : Controller
    {
        private OADbContext db = new OADbContext();

        // GET: Exams
        public ActionResult Index(int id)
        {
            var exams = db.Exams.Include(e => e.DepartmentGrade).Where(e=>e.DepartmentGradeID==id);
            Session["eid"] = id;
            return View(exams.ToList());
        }

        // GET: Exams/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Exam exam = db.Exams.Find(id);
            if (exam == null)
            {
                return HttpNotFound();
            }
            return View(exam);
        }

        // GET: Exams/Create
        public ActionResult Create()
        {
            ViewBag.DepartmentGradeID = new SelectList(db.DepartmentGrades, "ID", "Name");
            return View();
        }

        // POST: Exams/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,ExamName,DepartmentGradeID,ExamDate")] Exam exam)
        {
            if (ModelState.IsValid)
            {
                db.Exams.Add(exam);
                db.SaveChanges();
                return RedirectToAction("Index",new { id = exam.DepartmentGradeID });
            }

            ViewBag.DepartmentGradeID = new SelectList(db.DepartmentGrades, "ID", "Name", exam.DepartmentGradeID);
            return View(exam);
        }


        public ActionResult CreateExam()
        {
            ViewBag.DepartmentGradeID = new SelectList(db.DepartmentGrades, "ID", "Name");
           
            return View();
        }

        // POST: Exams/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CreateExam([Bind(Include = "ExamName,DepartmentGradeID,ExamDate")] CreateExam createExam)
        {
            
            var exam = new Exam();
           
            if (ModelState.IsValid)
            {
                exam.ExamName = createExam.ExamName;
                exam.DepartmentGradeID = createExam.DepartmentGradeID;
                exam.ExamDate = createExam.ExamDate;
                db.Exams.Add(exam);
                db.SaveChanges();
              
                return RedirectToAction("CreateExam2");
            }

            ViewBag.DepartmentGradeID = new SelectList(db.DepartmentGrades, "ID", "Name", createExam.DepartmentGradeID);
           
            return View(createExam);
        }
        public ActionResult CreateExam2()
        {
            ViewBag.CourseID = new SelectList(db.Courses, "ID", "Subject"); ;

            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CreateExam2([Bind(Include = "CourseID,FullMark")] CreateExam createExam)
        {
            
          
            var coursesexam = new CourseExam();
            var grade = new Grade();
            if (ModelState.IsValid)
            {
             
                Exam[] exams = db.Exams.ToArray();
                int eid = exams[exams.Length - 1].ID;
                int did = exams[exams.Length - 1].DepartmentGradeID;

                coursesexam.ExamID = eid;
                coursesexam.CourseID = createExam.CourseID;
                coursesexam.FullMark = createExam.FullMark;
                db.CourseExams.Add(coursesexam);
                db.SaveChanges();
                List<Student> Students = db.Students.Where(s => s.TeachingClass.DepartmentGradeID==did).ToList();
                
                    foreach (var student in Students)
                    {
                        grade.StudentID = student.ID;
                        grade.CourseID = createExam.CourseID;
                        grade.ExamID = eid;
                        grade.Score = 0;
                        db.Grades.Add(grade);

                        db.SaveChanges();
                    }


                
                return RedirectToAction("CreateExam2");
            }

            ViewBag.CourseID = new SelectList(db.Courses, "ID", "Subject", createExam.CourseID);
           return View();
        }
        // GET: Exams/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Exam exam = db.Exams.Find(id);
            if (exam == null)
            {
                return HttpNotFound();
            }
            ViewBag.DepartmentGradeID = new SelectList(db.DepartmentGrades, "ID", "Name", exam.DepartmentGradeID);
            return View(exam);
        }

        // POST: Exams/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,ExamName,DepartmentGradeID,ExamDate")] Exam exam)
        {
            if (ModelState.IsValid)
            {
                db.Entry(exam).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index", new { id = exam.DepartmentGradeID });
            }
            ViewBag.DepartmentGradeID = new SelectList(db.DepartmentGrades, "ID", "Name", exam.DepartmentGradeID);
            return View(exam);
        }

        // GET: Exams/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Exam exam = db.Exams.Find(id);
            db.Exams.Remove(exam);
            db.SaveChanges();
            return RedirectToAction("Index", new { id = exam.DepartmentGradeID });
            if (exam == null)
            {
                return HttpNotFound();
            }
            return View(exam);
        }

        // POST: Exams/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Exam exam = db.Exams.Find(id);
            db.Exams.Remove(exam);
            db.SaveChanges();
            return RedirectToAction("Index", new { id = exam.DepartmentGradeID });
        }
        public ActionResult Fbindex2()
        {
            return View();
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
